{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8a872427",
   "metadata": {},
   "source": [
    "------\n",
    "* 2023-10-23\n",
    "* 课程:python语言\n",
    "* week08课堂\n",
    "------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42e7ba42",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "## 1. 列表进阶\n",
    "## 2. 课本项目\n",
    "## 3. 课堂项目"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9a43493",
   "metadata": {},
   "source": [
    "## 1.1接上周内容（）列表的方法--remove"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "efa9f922",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalax']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list = [\"张三\",\"李四\",\"小明\",\"Jalax\"]\n",
    "\n",
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "257bcb89",
   "metadata": {},
   "source": [
    "* 思考，我怎么知道“李鹏”在数据列表里呢？\n",
    "\n",
    "> 1. 先查询\n",
    "> 2. 后删除\n",
    "\n",
    "* In方法(查询元素的方法)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b2dd995e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三\n"
     ]
    }
   ],
   "source": [
    "# 列表的循环\n",
    "for i in name_list:\n",
    "    if i == \"张三\":\n",
    "        print(i)\n",
    "        name_list.remove(\"张三\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c3577daf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李四', '小明', 'Jalax']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3b0126f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"小明\" in name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6bc929aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "if \"小明\" in name_list:\n",
    "    name_list.remove(\"小明\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a545b6de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李四', 'Jalax']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0daf0549",
   "metadata": {},
   "source": [
    "### 1.2 列表的其他方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b75b4a9e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李四', 'Jalax']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# clear 清空 ， 清除列表中的所有元素\n",
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3b630209",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fe0831be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a8726fa6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'jalex', '李四']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# count 查看list中元素出现的数量\n",
    "name_list = [\"张三\",\"李四\",\"小明\",\"jalex\",\"李四\"]\n",
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "62c98457",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.count(\"李四\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f1e320a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 案例\n",
    "# 假设掷骰子10000次，统计1-6各出现多少次\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "4e37b7ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 是不是需要现有一个空列表？来存放我们产生的数据呢？\n",
    "random_list = []\n",
    "# 增加数据到列表里面？\n",
    "for i in range(100):\n",
    "    random_list.append(random.randint(1,6))\n",
    "  #  print(random.randint(1,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "73190b93",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 2, 6, 6, 5, 4, 4, 3, 4, 1, 4, 6, 5, 6, 3, 2, 4, 4, 4, 4, 1, 2, 3, 4, 6, 4, 4, 4, 1, 3, 4, 3, 1, 1, 3, 3, 3, 1, 3, 3, 1, 4, 4, 2, 3, 3, 5, 5, 1, 4, 5, 4, 5, 5, 1, 1, 6, 1, 2, 5, 3, 5, 4, 5, 1, 6, 1, 2, 4, 4, 3, 1, 1, 5, 2, 4, 3, 2, 6, 5, 6, 5, 2, 2, 3, 6, 5, 5, 1, 4, 3, 2, 1, 4, 1, 5, 2, 6, 1, 1]\n"
     ]
    }
   ],
   "source": [
    "print(random_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "e0a307c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 0.2\n",
      "2 0.12\n",
      "3 0.18\n",
      "4 0.23\n",
      "5 0.16\n",
      "6 0.11\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,7):\n",
    "    print(i,random_list.count(i)/100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6711d66",
   "metadata": {},
   "source": [
    "### 1.3 列表元素的排序 sort()和reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "ca27c38f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'jalex', '李四']"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list = [\"张三\",\"李四\",\"小明\",\"jalex\",\"李四\"]\n",
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "4cb4d08c",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "62954cbd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jalex', '小明', '张三', '李四', '李四']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "494e9db5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[12, 13, 14, 15, 16]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list = [12,13,14,15,16]\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "f7504384",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[12, 13, 14, 15, 16]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.sort()\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "3ec7d87b",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list.reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "ef9b5323",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[16, 15, 14, 13, 12]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec212209",
   "metadata": {},
   "source": [
    "### 1.4 copy()\n",
    "\n",
    "> 1. =\n",
    "> 2. copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "9ba6e386",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3233155172864\n",
      "3233154366512\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'jalex', '李四']"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list01 = [\"张三\",\"李四\",\"小明\",\"jalex\",\"李四\"]\n",
    "print(id(name_list01))\n",
    "print(id(name_list01[0]))\n",
    "name_list01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "cce93b0b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3233153333632\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'jalex', '李四']"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list02 = name_list01\n",
    "print(id(name_list02))\n",
    "name_list02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "de17477b",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list02.append('Marry')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "a911d2fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'jalex', '李四', 'Marry']"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "03fcbaf2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'jalex', '李四', 'Marry']"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47d9b01e",
   "metadata": {},
   "source": [
    "* 如上原因：我们不得不找一个方法来实现“真正的复制”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "f856b006",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3233154824640\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'jalex', '李四']"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list01 = [\"张三\",\"李四\",\"小明\",\"jalex\",\"李四\"]\n",
    "print(id(name_list01))\n",
    "name_list01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "52cf1643",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3233154599744\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'jalex', '李四']"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list02 = name_list01.copy()\n",
    "name_list02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "5983023d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李四'"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list01.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "aab8982e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'jalex']"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "28d46b66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'jalex', '李四']"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "609a7cf9",
   "metadata": {},
   "source": [
    "## 实践：购物车项目\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f76019c7",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "购物车程序项目：\n",
    "\n",
    "> 1. 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户\n",
    "> 2. 让用户输入商品编号进行商品的购买\n",
    "> 3. 用户选择商品后，检查用户的余额是否足够，若够则直接扣款，不够则提醒用户\n",
    "> 4. 用户可以随时购买，推出时打印用户已购买的商品和支付宝余额\n",
    "\n",
    "----"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a9c15e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入用户名：siqi\n",
      "请输入密码：123\n",
      "请输入您的余额：30\n",
      "欢迎光临丢丢小店！您将获得4次购买机会~\n",
      "商品以及编号如下：\n",
      "0 ('巧克力饼干', 10)\n",
      "1 ('巧克力薯条', 20)\n",
      "2 ('巧克力牛奶', 20)\n",
      "3 ('巧克力糖果', 10)\n",
      "请输入您要购买的商品编号：2\n",
      "您想要购买的商品是： ('巧克力牛奶', 20)\n",
      "检查到您的余额还有： 30 ,即将执行扣款。\n",
      "您成功购买的商品是： ('巧克力牛奶', 20)\n",
      "您的余额还有： 10\n",
      "恭喜您获得商品！ 您还有3次购买机会喔~\n"
     ]
    }
   ],
   "source": [
    "# 购物车程序项目\n",
    "\n",
    "#商品清单\n",
    "list = [(\"巧克力饼干\", 10), (\"巧克力薯条\", 20), (\"巧克力牛奶\", 20), (\"巧克力糖果\", 10)] \n",
    "\n",
    "#用户登录与余额\n",
    "user_name = input(\"请输入用户名：\")\n",
    "password = input(\"请输入密码：\")\n",
    "\n",
    "balance = int(input(\"请输入您的余额：\"))\n",
    "\n",
    "#进入购物车并展示商品\n",
    "print(\"欢迎光临丢丢小店！您将获得4次购买机会~\")\n",
    "count = 4\n",
    "print(\"商品以及编号如下：\")\n",
    "\n",
    "for i in list:\n",
    "    print(list.index(i), i)\n",
    "\n",
    "while True: \n",
    "    count -=1\n",
    "#购买机会\n",
    "    if count == 0:\n",
    "        print(\"购买机会已用完！欢迎下次光临~\")\n",
    "        break\n",
    "    else:\n",
    "        number = input(\"请输入您要购买的商品编号：\")\n",
    "        choice = list[int(number)]  \n",
    "        print(\"您想要购买的商品是：\",choice)\n",
    "        if  balance>10:\n",
    "            print(\"检查到您的余额还有：\",balance,\",即将执行扣款。\")\n",
    "            balance02 = balance - choice[1]\n",
    "            print(\"您成功购买的商品是：\",choice)\n",
    "            print(\"您的余额还有：\",balance02)\n",
    "            print(\"恭喜您获得商品！\",\"您还有%s次购买机会喔~\"%(count))\n",
    "        else:\n",
    "            print(\"很遗憾！您的余额不足，无法购买商品！请充值！\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "402b5f2d",
   "metadata": {},
   "source": [
    "# "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59091f72",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "955e0a08",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47a062fa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
